<?php
//
//	file: includes/acp/acp_attach_config_srv.php
//	author: ptirhiik
//	begin: 24/02/2006
//	version: 1.6.1 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Attach_settings';
$cp_title_explain = 'Manage_attachments_explain';

// no fields in form : end there
if ( empty($fields) )
{
	// send "no options" with left side menus
	$cp_panels->display_empty();
	return;
}

// we need the two file_handler layer
$file_handler_layer = !intval($config->globals['mod_attachment_CH']['config']->data['allow_ftp_upload']) ? 'includes/attach/class_file_handler_ftp' : 'includes/attach/class_file_handler_copy';
include($config->url($file_handler_layer));
unset($file_handler_layer);

class attach_config_srv_auto_form extends auto_form
{
	function init()
	{
		parent::init();
		$this->check_ftp();
	}

	function validate()
	{
		global $warning, $warning_msg;
		if ( $warning )
		{
			return;
		}
		parent::validate();
	}

	function display()
	{
		global $template, $config, $user;
		global $warning, $warning_msg;

		if ( $warning )
		{
			$template->assign_block_vars('warning', array(
				'WARNING_TITLE' => $user->lang('Information'),
				'WARNING_MSG' => $user->lang($warning_msg),
			));
		}
		parent::display();
	}

	function check_ftp()
	{
		global $error, $error_msg;
		global $warning, $warning_msg;
		global $config, $user;

		if ( !_button('check_settings') )
		{
			return;
		}

		// verify the fields are correctly inputed
		$this->check();
		if ( !$error )
		{
			$ftp_settings = array(
				'allow_ftp_upload' => $this->fields['allow_ftp_upload']->value,
				'ftp_server' => $this->fields['ftp_server']->value,
				'ftp_port' => $this->fields['ftp_port']->value,
				'ftp_path' => $this->fields['ftp_path']->value,
				'ftp_pasv_mode' => $this->fields['ftp_pasv_mode']->value,
				'ftp_user' => $this->fields['ftp_user']->value,
				'ftp_pass' => $this->fields['ftp_pass']->value,
				'upload_dir' => $this->fields['upload_dir']->value,
			);
			$file_handler = new file_handler($ftp_settings);
			if ( !$file_handler->check_settings() )
			{
				_warning($file_handler->error());
			}
		}

		// halt on error
		if ( $error )
		{
			message_return($error_msg, $this->return_msg, $config->url($this->requester, $this->return_parms, true), 10);
		}
		else if ( !$warning )
		{
			_warning('Test_settings_successful');
			_warning('Click_submit_to_save');
		}
	}

	function update_table(&$form_fields)
	{
		global $db, $config;

		if ( empty($form_fields) )
		{
			return;
		}
		$keys = array();
		$db->sql_stack_reset();
		foreach ( $form_fields as $name => $value )
		{
			$keys[] = $db->sql_escape_string($name);
			$fields = array(
				'config_name' => $name,
				'config_value' => $value,
			);
			$db->sql_stack_statement($fields);
		}
		$sql = 'DELETE FROM ' . ATT_CONFIG_TABLE . '
					WHERE config_name IN(\'' . implode('\', \'', $keys) . '\')';
		$db->sql_query($sql, false, __LINE__, __FILE__);
		$db->sql_stack_insert(ATT_CONFIG_TABLE, false, __LINE__, __FILE__);

		// recache config
		$config->globals['mod_attachment_CH']['config']->read(true);

		// send achievement message
		message_return('Attach_config_updated', $this->return_msg, $config->url($this->requester, $this->return_parms, true));
	}
}

// instantiate the form
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
);
$form = new attach_config_srv_auto_form($config->globals['mod_attachment_CH']['config']->data, $fields, $cp_requester, 'Click_return_' . $ctx_id, $cp_parms + $parms);
$form->process();
$template->set_switch('form');
$template->set_switch('free_size');
$template->set_filenames(array('body' => 'cp_generic.tpl'));

// add the buttons
$buttons = array(
	'check_settings' => array('txt' => 'Test_settings', 'img' => 'cmd_check', 'key' => 'cmd_check'),
);
display_buttons($buttons);

?>